package com.boot.service;

import com.boot.entity.User;
import com.boot.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserService {

    @Autowired
    UserMapper userMapper;

    public List<User> list(){
        return userMapper.list();
    }

    public Boolean login(String userName, String password) {
        List<User> users = userMapper.getByUserNameAndPassword(userName,password);
        return users!=null && users.size()==1;
    }

    public Boolean deleteById(Long id) {
        return userMapper.deleteById(id) == 1;
    }

    public User getById(Long id) {
        return userMapper.getById(id);
    }

    public Boolean saveOrUpdate(User user) {
        if(user!=null && user.getId()!=null){
            user.setAddTime(new Date());
            return userMapper.updateById(user) == 1;
        }
        if(user!=null && user.getId()==null){
            return userMapper.save(user) == 1;
        }
        return false;
    }
}

